package cn.tedu.charging.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();



    //序列化为json
    public static String  toJson(Object data){
        try {
            String str = objectMapper.writeValueAsString(data);
            return str;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    //反序列化为对象
    public static <O> O toObject(String json,Class<O> oClass ){
        try {
            O o = objectMapper.readValue(json,oClass);
            return o;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
